// packageF/timeAppointment/timeAppointment_index/timeAppointment_index.js
var location = require("../../../mybehaviors/location/location");
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [location,skinBehavior],
  data: {
    showServicePopup:false,
    carousels:[],
    cservice:{},
    store_info:{},
    recommand_obj_list:[],

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    sort_order_field:'',
    sort_order_by:'',
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getGoodsList();
    this._getLocation((mapdata,e)=>{
      this.setData({
        address: mapdata.address,
        city: mapdata.address_component.city ? mapdata.address_component.city : mapdata.address_component.province,
        point: e
      });
      this.getData();
    });
  },

  openService(){
    this.setData({
      showServicePopup:!this.data.showServicePopup
    });
  },

  gotoGoods(e){
    let id = e.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id='+id,
    });
  },

  getData(){
    let point = app.qqMapTransBMap(this.data.point.lng,this.data.point.lat);
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.index.get-data");
    app._getNetWork({
      url: urlStr,
      data:{
        lat:point.lat,
        lng:point.lng
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        console.log(res);
        this.setData({
          cservice:res.data.cservice,
          carousels:res.data.carousels,
          store_info:res.data.store_info,
          recommand_obj_list:res.data.recommand_obj_list
        });
      }
    });
  },

  gotoUserDe(evt){
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageF/timeAppointment/AppointmentUserDe/AppointmentUserDe?id='+id,
    });
  },

  setSort(evt){
    this.setData({
      sort_order_field:evt.detail.order_field,
      sort_order_by:evt.detail.order_by
    });
    this.getGoodsList();
  },
  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false,
    });
  },
  getGoodsList(){
    this.initData();
    let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.goods.search-goods");
    let json={};
    if(this.data.sort_order_field!=''){
      json.order_field = this.data.sort_order_field;
    }
    if(this.data.sort_order_by!=''){
      json.order_by = this.data.sort_order_by;
    }
    app._getNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        console.log(res);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData:res.data.data,
          networkLoading:true
        });
      }
    });
  },

  gotoSearch(){
    wx.navigateTo({
      url: '/packageF/timeAppointment/timeAppointment_search/timeAppointment_search',
    });
  },
  gotoRecommend(){
    wx.navigateTo({
      url: '/packageF/timeAppointment/timeAppointment_search/timeAppointment_search?status=2',
    });
  },

  tolocation() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const url = `/${currentPage.route}?page_id=${this.data.page_id}`;
    wx.setStorageSync('diyurl', url);
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=shop'
    });

  },

  goToAdress() {
    let latitude = this.data.store_info.lat;
    let longitude = this.data.store_info.lng;
    let store_name = this.data.store_info.name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: store_name
    });
  },

  tapUrl(e) {
    let link = e.currentTarget.dataset.url;
    if (link == "") return false;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) {
      console.log(err);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let json={ page: this.data.page };
      if(this.data.sort_order_field!=''){
        json.order_field = this.data.sort_order_field;
      }
      if(this.data.sort_order_by!=''){
        json.order_by = this.data.sort_order_by;
      }
      let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.goods.search-goods");
      app._postNetWork({
        url: urlStr,
        data: json, 
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});